﻿using System;
using System.Windows.Forms;
using Icon.Properties;
using Microsoft.Win32;

namespace Icon
{
    public partial class Main : Form
    {
       
        public Main()
        {
            InitializeComponent();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            WindowsHelper.Show(ptr);
        }

        IntPtr ptr= IntPtr.Zero;

        private void TestButton_Click(object sender, EventArgs e)
        {
            ptr= WindowsHelper.FindMainWindowByProc(Settings.Default.exe);
            WindowsHelper.Hide(ptr);
        }

        private void TestButton2_Click(object sender, EventArgs e)
        {
            WindowsHelper.Show(ptr);
        }

        private void 开机启动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run",true);
            
            if (key == null) return;

            if (((ToolStripMenuItem)sender).Checked)
                key.SetValue("IconServer", Application.ExecutablePath);
            else
                key.DeleteValue("IconServer");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ptr = WindowsHelper.FindMainWindowByProc(Settings.Default.exe);
            if(ptr!=IntPtr.Zero)
            {
                WindowsHelper.Hide(ptr);
                timer1.Enabled = false;
            }
            timer1.Interval = timer1.Interval*2;
        }

        private void Main_Load(object sender, EventArgs e)
        {
            var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
            if(key!=null)
            {
                if(key.GetValue("IconServer")!=null)
                {
                    开机启动ToolStripMenuItem.Checked = true;
                }
            }
        }
    }
}